package com.mission.schedule.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.mission.schedule.activity.MainActivity;
import com.mission.schedule.activity.NewFriendsActivity;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.NoteTypeBean;
import com.mission.schedule.bean.qd606.NoteShareBean;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.SharedPrefUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 下载和同步清单
 * Created by liwei on 2017/3/2.
 */

public class NoteDService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopSelf();
        App.getHttpQueues().cancelAll("downnote");
    }


    SharedPrefUtil sharedPrefUtil;
    String UserId = "";
    String downtime = "2017-01-01";

    @Override
    public void onStart(final Intent intent, int startId) {
        super.onStart(intent, startId);

        sharedPrefUtil = new SharedPrefUtil(getApplication(),
                ShareFile.USERFILE);
        UserId = sharedPrefUtil.getString(getApplicationContext(),
                ShareFile.USERFILE, ShareFile.USERID, "");

        new Thread(new Runnable() {
            @Override
            public void run() {
                downNoteData();
            }
        }).start();
    }

    /**
     * 清单下载
     */
    private synchronized void downNoteData() {
        String result = sharedPrefUtil.getString(getApplicationContext(),
                ShareFile.USERFILE, ShareFile.LOGINNOTEDATA, "");
//        if (NetUtil.getConnectState(getApplication()) != NetUtil.NetWorkState.NONE) {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    StringRequest stringRequest = new StringRequest(Method.POST, URLConstants.清单和清单标题下行,
//                            new Response.Listener<String>() {
//                                @Override
//                                public void onResponse(String result) {
                                    if (!TextUtils.isEmpty(result)) {
                                        try {
                                            Gson gson = new Gson();
                                            NoteTitleDetailBean bean = gson.fromJson(result, NoteTitleDetailBean.class);
                                            if (bean.status == 0) {
//                                                sharedPrefUtil.putString(
//                                                        getApplication(),
//                                                        ShareFile.USERFILE,
//                                                        ShareFile.UPDATENOTESETTIME,
//                                                        bean.downTime.replace('T', ' '));
                                                List<NoteTitleDetailBean.TDelListBean> tDelList = bean.tDelList;
                                                List<NoteTitleDetailBean.ListBean> beanList = bean.list;
                                                List<NoteTitleDetailBean.DelListBean> delList = bean.delList;

//                                                if (tDelList != null) {
//                                                    boolean isTitleBWL = false;
//                                                    for (NoteTitleDetailBean.TDelListBean tDelListBean : tDelList) {
//                                                        if (tDelListBean.titles.equals("备忘录")) {
//                                                            if (!isTitleBWL) {
//                                                                App.getDBcApplication().saveNoteTitleData(tDelListBean);
//                                                            }
//                                                            isTitleBWL = true;
//                                                        } else {
//                                                            App.getDBcApplication().saveNoteTitleData(tDelListBean);
//                                                        }
//                                                    }
//                                                }

                                                if (beanList != null) {
                                                    for (NoteTitleDetailBean.ListBean listBean : beanList) {
                                                        if (!listBean.contents.isEmpty()) {
                                                            App.getDBcApplication().saveNoteDetailData(listBean);
                                                        }
                                                    }
                                                }
                                                sharedPrefUtil.putString(getApplicationContext(),
                                                        ShareFile.USERFILE, ShareFile.LOGINNOTEDATA, "");

//                                                if (delList != null) {
//                                                    for (NoteTitleDetailBean.DelListBean delListBean : delList) {
//
//                                                        App.getDBcApplication().deleteNote(delListBean.type, delListBean.dataId);
//                                                    }
//                                                }
                                            }
                                        } catch (JsonSyntaxException e) {
                                            e.printStackTrace();
                                        }
                                    }
//                                }
//
//                            }, new Response.ErrorListener() {
//                        @Override
//                        public void onErrorResponse(VolleyError volleyError) {
//                            Log.d("TAG", "error:" + volleyError.toString());
//                        }
//                    }) {
//                        @Override
//                        protected Map<String, String> getParams() throws AuthFailureError {
//                            Map<String, String> map = new HashMap<>();
//                            map.put("uId", UserId);// "52432"
//                            map.put("changeTime", "2017-01-01");//downtime.replace('T', ' ')
//                            return map;
//                        }
//                    };
//                    stringRequest.setTag("downnote");
//                    stringRequest.setRetryPolicy(new DefaultRetryPolicy(10000, 1, 1.0f));
//                    App.queues.add(stringRequest);
//                }
//            }).start();
//        }
    }

}
